package com.ziq.zcamp.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ziq.base.extension.BindingViewHolder
import com.ziq.base.extension.click
import com.ziq.base.extension.createBindingViewHolder
import com.ziq.zcamp.databinding.DialogCommentActionBinding
import com.ziq.zcamp.databinding.ItemDialogCommentActionBinding

class CommentActionPopupWindow(context: Context, actionList: List<String>): PopupWindow(context) {

    var binding : DialogCommentActionBinding = DialogCommentActionBinding.inflate(LayoutInflater.from(context))
    var actionAdapter: ActionAdapter? = null
    init {
        contentView = binding.root
        setBackgroundDrawable(null)
        actionAdapter = ActionAdapter()
        binding.rvAction.layoutManager = LinearLayoutManager(
            context,
            RecyclerView.HORIZONTAL,
            false)
        binding.rvAction.adapter = actionAdapter
        actionAdapter?.setData(actionList)
    }

    var actionCallBack : ((action:String)->Unit)? = null

    fun setActionCallback(callback : (action:String)->Unit){
        actionCallBack = callback
    }

    inner class ActionAdapter() : RecyclerView.Adapter<BindingViewHolder<ItemDialogCommentActionBinding>>() {

        private val dataList: MutableList<String> = mutableListOf()
        fun setData(list: List<String>) {
            dataList.clear()
            dataList.addAll(list)
            notifyDataSetChanged()
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): BindingViewHolder<ItemDialogCommentActionBinding> {
            return createBindingViewHolder(parent)
        }

        override fun onBindViewHolder(
            holder: BindingViewHolder<ItemDialogCommentActionBinding>,
            position: Int
        ) {
            val item = dataList[position]
            holder.binding.btnAction.text = item
            holder.itemView.click {
                dismiss()
                actionCallBack?.invoke(item)
            }
        }
    }

}